<?php
namespace app\shop\home;
use app\shop\model\Banner;
use app\shop\model\Artwork;
use app\admin\model\Attachment;
use app\shop\model\Address;
use app\shop\model\User;
use app\shop\model\Order as OrderModel;
use app\shop\model\OrderList;

class Order extends Common
{
	public function index()
	{	
		//获取商品详情
		$artwork = Artwork::where('id',request()->param('artwork_id'))->find();
    	//pr($artwork->toArray());die;
        $images = explode(',', $artwork['photos']);
        $imgs = Attachment::where('id','in',$images)->field('path')->select();
        //获取收货地址
        $address = Address::where(['user_id'=>session('auction_user')['id']])->select();
        $address = isset($address[0]) ? $address[0] : [];

        $this->assign('artwork', $artwork);
        $this->assign('img', $imgs[0]);
		$this->assign('address', $address);
		if(isMobile()){
            return $this->fetch('Mobile/Order/index');
        }else{
            return $this->fetch();
        }
	}

	public function ajaxOrder()
	{
		$data = request()->param();
		
        $userInfo = User::where('id',session("auction_user")['id'])->find()->toArray();

        $is_show = Artwork::where('id',$data['arwork_id'])->field('sale_out')->find();

        if($is_show['sale_out'] ==1){
        	echo json_encode(['code'=>4,'msg'=>'该作品已售出']);exit;
        }

		if(isset($userInfo['money']) && !$userInfo['money']){
            $userInfo['money'] = '0.00';
        }

        $yuer = $data['price']/10;
        if($yuer > $userInfo['money']){
        	echo json_encode(['code'=>1,'msg'=>'余额不足应付金额的10%，请先去充值']);exit;
        }
        if($data['pay_type']==2 && $data['price']>$userInfo['money']){
			echo json_encode(['code'=>2,'msg'=>'余额不足请先去充值']);exit;
        }
        if($data['pay_type']==2){
        	$yuer = $data['price'];
        	$userData['money'] = $userInfo['money']-$yuer;
        }else{
        	$userData['money'] = $userInfo['money']-$yuer;
        }
        $userData['id'] = session("auction_user")['id'];
        $orderNum = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        User::startTrans();
        $res = User::where('id',$userData['id'])->update($userData);
        if($res){
        	$orderData['status'] = $data['pay_type']==2 ? 2 : 1;
			$orderData['price'] = $data['price'];
			$orderData['address_id'] = $data['address_id'];
			$orderData['send_type'] = $data['send_type'];
			$orderData['pay_type'] = $data['pay_type'];
			$orderData['pack_type'] = $data['pack_type'];
			$orderData['remark'] = $data['remark'];
			$orderData['order_num'] = $orderNum;
			$orderData['user_id'] = $userData['id'];
			$orderData['add_name'] = $data['addrname'];
			$orderData['add_address'] = $data['address'];
			$orderData['add_tel'] = $data['mobile'];
			$orderData['real_price'] = $yuer;
			$orderData['create_time'] =time();
			$orderData['update_time'] =time();
			$res1 = OrderModel::insertGetId($orderData);
			if($res1){
				$orderListData['order_id'] = $res1;
				$orderListData['status'] = 1;
				$orderListData['title'] = $data['arwork_title'];
				$orderListData['price'] = $data['price'];
				$orderListData['create_time'] = time();
				$orderListData['artwork_id'] = $data['arwork_id'];
				$res2 = OrderList::insertGetId($orderListData);
				if($res2){
					$productData['id'] = $data['arwork_id'];
					$productData['sale_out'] = 1;
					$res3 = Artwork::where('id',$data['arwork_id'])->update($productData);
					if($res3){
						User::commit();
						echo json_encode(['code'=>0,'msg'=>'下单成功！','data'=>url('shop/order/detail',['id'=>$res1])]);exit;
					}else{
						User::rollback();
						echo json_encode(['code'=>3,'msg'=>'下单失败，请稍后重试']);exit;
					}
					
				}else{
					User::rollback();
					echo json_encode(['code'=>3,'msg'=>'订单生成失败，请稍后重试']);exit;
				}
			}else{
				User::rollback();
        		echo json_encode(['code'=>3,'msg'=>'订单生成失败，请稍后重试']);exit;
			}
        }else{
        	User::rollback();
        	echo json_encode(['code'=>3,'msg'=>'扣款失败，请稍后重试']);exit;
        }

        
	}

	public function detail()
	{
		$id = request()->param('id');
		$field = 'id,price,real_price,send_type,pay_type,order_num';
		$orderInfo = OrderModel::where('id',$id)->field($field)->find()->toArray();
		if($orderInfo['send_type']==1){
			$orderInfo['send_type'] ='普通快递';
		}elseif($orderInfo['send_type']==2){
			$orderInfo['send_type'] ='自提';
		}elseif($orderInfo['send_type']==3){
			$orderInfo['send_type'] ='物流';
		}else{
			$orderInfo['send_type'] ='同城送货';
		}
		if($orderInfo['pay_type']==1){
			$orderInfo['pay_type_name'] ='微信支付';
		}elseif($orderInfo['pay_type']==2){
			$orderInfo['pay_type_name'] ='余额支付';
		}elseif($orderInfo['pay_type']==3){
			$orderInfo['pay_type_name'] ='在线网银转账';
		}else{
			$orderInfo['pay_type_name'] ='对公银行转账';
		}
		$orderInfo['pay_yuer'] = $orderInfo['price']-$orderInfo['real_price'];
		//pr($orderInfo);die;
		$this->assign('orderInfo', $orderInfo);
		if(isMobile()){
            return $this->fetch('Mobile/Order/detail');
        }else{
            return $this->fetch();
        }
	}
}